#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <SDL/SDL.h>

#include "headers/FileLoader.h"

#include "headers/Windows.h"
#include "headers/PPUSystem.h"
#include "headers/LevelMenager.h"

#include "headers/Callbacks.h"

#include "headers/Fonts.h"

FileLoader cFileLoader;
PPUSystem cPPUSystem;
LevelMenager cLevelMenager;
WindowsMenager cWindowsMenager;
SquaresList cSquaresList(10);

#include "headers/Enemies.h"
Enemies cEnemies;


SDL_Surface* screen = NULL;

const char WindowTitle[] = "DizzyEdit ver. alpha alpha alpha";


int InitAll();
static void draw();


int main (int argc, char* argv[])
{
    char* msg;
    int done;

    /* Initialize SDL */
    if (SDL_Init (SDL_INIT_VIDEO) < 0)
    {
        sprintf (msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());
        //MessageBox (0, msg, "Error", MB_ICONHAND); 
        free (msg);
        exit (1);
    }
    atexit (SDL_Quit);

    /* Set 640x480 16-bits video mode */
    screen = SDL_SetVideoMode (640, 460, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL)
    {
        sprintf (msg, "Couldn't set 640x460x16 video mode: %s\n",
          SDL_GetError ());
        //MessageBox (0, msg, "Error", MB_ICONHAND); 
        free (msg);
        exit (2);
    }
    SDL_WM_SetCaption (WindowTitle, NULL);
    InitText();
 
    InitAll();
    
    done = 0;
       
    while (!done)
    {
        SDL_Event event;

        /* Check for events */
        while (SDL_PollEvent (&event))
        {
            switch (event.type)
            {
                case SDL_KEYDOWN:
                    switch (event.key.keysym.sym) {
                        case SDLK_ESCAPE:
                            done = 1; break;
                        case SDLK_LEFT: {
                            int lvl = cLevelMenager.GetCurrentLvl()-1;
                            if (lvl<0) break;
                            if (lvl==0x10) lvl=0xf;                             
                            cLevelMenager.LoadLevel(lvl); break;
                        }
                        case SDLK_RIGHT: {
                            int lvl = cLevelMenager.GetCurrentLvl()+1;
                            if (lvl>0x31) break;
                            if (lvl==0x10) lvl=0x11;
                            cLevelMenager.LoadLevel(lvl); break;
                        }
                    }                             
                    break;
                    
                case SDL_MOUSEBUTTONDOWN: {
                    int x = event.motion.x;
                    int y = event.motion.y;  
                    
                    cWindowsMenager.MouseClick(x, y, 0);
                    
                    break;
                }
                
                case SDL_MOUSEBUTTONUP:
                    cWindowsMenager.MouseUp();
                    break;
                
                case SDL_QUIT:
                    done = 1;
                    break;
                    
                default:
                    break;
            } // switch (event.type)
        } // while (SDL_PollEvent (&event))
        
        /* Draw to screen */
        draw ();
    }
    cFileLoader.SaveToFile("Dizzy kopia.nes");
    //cFileLoader.SaveBanks();
    DeinitText();
    
    return 0;
}


int InitAll() {
    
    cFileLoader.LoadFile("Dizzy.nes");
    cPPUSystem.Init();
        
    /* Level */    
    cWindowsMenager.AddWindow("Level", ALL_FRAMES, MouseClickLevelMenager);
    cWindowsMenager.SetDestX("Level", 16);
    cWindowsMenager.SetDestY("Level", 16);
    
    /* Squares List : */
    cWindowsMenager.AddWindow("SquaresList", ALL_FRAMES, 338, 1000, MouseClickSquaresList);
    cWindowsMenager.SetDestX("SquaresList", 16);
    cWindowsMenager.SetDestY("SquaresList", 232);
    cWindowsMenager.SetDestH("SquaresList", 200);
    
    
    cLevelMenager.SetDrawObjectName("Level");
    cEnemies.SetWindowName("Level");
    
    
    cLevelMenager.LoadLevel(0);
    
    /* Update window title */
    std::ostringstream TitleStream;
    TitleStream << WindowTitle << " - " << cLevelMenager.GetLevelName();
    SDL_WM_SetCaption (TitleStream.str().c_str(), NULL);
    
    cWindowsMenager.SetDestW("Level", 380);
    cWindowsMenager.SetDestH("Level", cLevelMenager.GetPixelsY());

}   

static void draw() {
    
    cLevelMenager.DrawFrame();
    cEnemies.Draw();
    cSquaresList.DrawFrame("SquaresList");
    cWindowsMenager.DrawAll();

    SDL_Flip (screen);
    /* Don't run too fast */
    SDL_Delay (20);
}
